/**
 * Created by admin on 2017/6/19.
 */
import {
  ChubIndex,
  ChubAct,
  ChubDetail,
  ChutCommentsList,
  ChutGoods
} from '../mutation-types';
import {setPageTitle} from '../../utils/util.js';
import {  Cache, cache_keys } from '../../utils/util';
export const queryChubDetail = function ({commit}, id) {

  // let remove=EventBus.$Message.loading({text:"加载中..."});

  EventBus.$http.get(EventBus.$Config.chubUrl+ 'api/admin/cms/contenthub/detail/'+id).then(res=>{
    // EventBus.$Loading.done();
    if(res.status){
      EventBus.title={
        1:'测评详情',
        2:'指南详情'
      }[res.data.data.content_type];
      setPageTitle(EventBus.title);

      var goods=res.data.data.goods_list;
      for(var i in goods){
        var str=`<ul class='chub_goods'>
                      <a href='#!/store/detail/${goods[i].id}' style="color: black">
                            <li class='mx-1px'>
                                   <div class='left'><img class='blockImg' src="${goods[i].img}"></div>
                                       <div class='right'>
                                       <h2 class='vmc-nowrap-3'>${goods[i].name}</h2>
                                        <div class='buy'>
                                         <div class='price'>¥${goods[i].sell_price}</div>
                                         <button>去购买</button>
                                        </div>
                                       </div>
                           </li>
                       </a>
                    </ul>`;
        res.data.data.content=res.data.data.content.replace("<p>*goods*</p>",str);

      }
      commit(ChubDetail, res.data.data);
      EventBus.$emit('set-title', EventBus.title);
      EventBus.$Indicator.remove();
    }
    else{
      EventBus.$Indicator.remove();
      EventBus.$Modal.info({title:"消息提示",body:res.data.message || '未知错误'});
    }
  },()=>{
    EventBus.$Indicator.remove();
    EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
  });
};
export const queryChubList = function ({commit},data) {
  let content_type=data.number || 1;
  let index =data.type || 0;
  let i=data.index;
  let page=data.page || 1;
  let title = {
    1: '评测',
    2: '指南',
    3: '活动'
  }[content_type];
  setPageTitle(title);
  EventBus.$emit("set-Listtitle",title);
  // const activity_type = this.tabList[index].activity_type;
  const activity_type= index;

  EventBus.$http.get(EventBus.$Config.chubUrl+'api/admin/cms/contenthub/articles/' + content_type +'/'+ activity_type +'/' + page ).then(res => {
    console.log(res.data);
    EventBus.$Indicator.remove();
    if(res.data.status){
      EventBus.$emit('complete-chubList', res.data, i,data.method);
    }else{
      EventBus.$Modal.info( {body:res.data.message || '未知错误'});
    }

  }, () => {
    // this.$Loading.done();
    EventBus.$Modal.info({body:'加载失败'});
    EventBus.$Indicator.remove();
  })
};
export const queryChubFavoState = function ({commit}, id) {

  // 拿自己本地
  const oauth = Cache.get(cache_keys.token);
  //
  EventBus.$http.get(EventBus.$Config.chubUrl+ 'api/admin/cms/contenthub/collect/check/' + id , {
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res => {
    // res = res.json();
    EventBus.$emit('complete-queryFavo', res.data.status);

  },() => {
    EventBus.$Modal.info({body:'加载失败'});
  })
};


export const submitChutFavo = function ({commit},data) {
  const oauth = Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.chubUrl+ 'api/admin/cms/contenthub/collect', {
    article_id: data.id,
    action: data.action
  }, {
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res => {
    if(res.data.status){
      EventBus.$emit('complete-submitFavo', res.data.data.status);
    }else{
      EventBus.$Modal.info( {body:res.data.message || '收藏失败'});
    }

  },() => {
    EventBus.$Modal.info( {body: '加载失败'});
  })
};

export const queryChutGoods =function({commit},article_id){
  EventBus.$Indicator.blade();
  EventBus.$http.get(EventBus.$Config.chubUrl+'api/admin/cms/contenthub/detailed/list/'+article_id).then( res=>{
    if(res.data.status){
      commit(ChutGoods,res.data.data);
      EventBus.$Indicator.remove();
    }
    else{
      EventBus.$Modal.info( {body:res.data.message || '未知错误'});
      EventBus.$Indicator.remove();
    }
  },()=>{
    EventBus.$Indicator.remove();
    EventBus.$Modal.info( {body:'加载失败'});
  });
}

export const queryChutCommentsList =function({commit,state},datas){
  let article_id=datas.id;
  let page=datas.page || 1;
  EventBus.$http.get(EventBus.$Config.chubUrl+'api/admin/cms/contenthub/comment/list/'+article_id+'?page='+page).then(res=>{
    if(res.data.status){
      let data =res.data.data;
      if(datas.page>1)
      {
        data =state.chutCommentsList.concat(data);
      }
      commit(ChutCommentsList,data);
      if(res.data.meta && res.data.meta.pagination)
      {
        EventBus.$emit('complete-page',true,res.data.meta.pagination,data.method);
      }
      else{
        EventBus.$emit('complete-page',false,"",datas.method);
      }
    }
    else{
      EventBus.$Modal.info( {body:res.data.message || '未知错误'});
    }
  },()=>{
    EventBus.$Modal.info( {body:'加载失败'});
  })
}

export const submitChutComments =function({commit},datas){
  const oauth =Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.chubUrl+'api/admin/cms/contenthub/comment',{
    article_id:datas.article_id,
    comment:datas.comment
  },{
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res =>{
    if(res.data.status){
      EventBus.$emit('complete-submit-comment');
    }
    else{
      EventBus.$Modal.info({body:res.data.message || '评论失败'});
    }
  },()=>{
    EventBus.$Modal.info({body:'加载失败'});
  })
}

export const queryChutActivityType =function({commit,state}){
  EventBus.$http.get(EventBus.$Config.chubUrl+'api/admin/cms/contenthub/activity/type/list').then(res =>{
    if(res.data.status){
      EventBus.$emit('complete-activity-type',res.data.data);
    }
    else{
      EventBus.$Modal.info({title:'消息提示',body:res.data.message || "未知错误"});
    }
  },()=>{
    EventBus.$Modal.info({title:'消息提示',body:'加载失败'});
  });
}

export const queryChubListActivity =function({commit,state},data){
  let content_type=3;
  let index =data.type || 0;
  let i=data.index;
  let page=data.page || 1;
  let tabTitle=data.title || "野练";
  let title = {
    1: '评测',
    2: '指南',
    3: '活动'
  }[content_type];
  setPageTitle(title);
  EventBus.$emit("set-Listtitle",title);
  // const activity_type = this.tabList[index].activity_type;
  const activity_type= index;
  if(title=="活动" && tabTitle=="野练"){

    EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/list/all',{
      params:{
        page:page
      }
    }).then(res => {
      console.log(res.data);
      if(res.data.status){
        EventBus.$emit('complete-chubListActivity', res.data, i);
      }else{
        EventBus.$Modal.info( {body:res.data.message || '未知错误'});
      }
      EventBus.$Indicator.remove();
    }, () => {
      // this.$Loading.done();
      EventBus.$Modal.info({body:'加载失败'});
      EventBus.$Indicator.remove();
    })
  }
  else if(title=="活动" && tabTitle!="野练"){
    EventBus.$http.get('https://admin.beta.tnf.ibrand.cc/api/admin/cms/contenthub/drupal/'+tabTitle+'/'+page).then(res => {
      console.log(res.data);
      if(res.data.data){
        EventBus.$emit('complete-chubDrupelActivity', res.data.status, i);
      }else{
        EventBus.$Modal.info( {body:res.data.message || '未知错误'});
      }
      EventBus.$Indicator.remove();
    }, () => {
      // this.$Loading.done();
      EventBus.$Modal.info({body:'加载失败'});
      EventBus.$Indicator.remove();
    })
  }
}
